﻿using DotNetStar.TheKing.Domain;
using DotNetStar.TheKing.Infrastructure;
using DotNetStar.TheKing.Services;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace DotNetStar.TheKing.Web
{
    /// <summary>
    /// 问答平台的首页
    /// </summary>
    public class ContentIndexAskViewComponent : ViewComponent
    {
        private readonly ContentService _contentService;
        public ContentIndexAskViewComponent(ContentService contentService)
        {
            _contentService = contentService;
        }

        public async Task<IViewComponentResult> InvokeAsync(int? categoryId)
        {            
            //未解决问题列表
            ViewData["NotAnswerList"] = _contentService.GetTopContentQueable(new ContentQuery { Channel = Channels.Ask })
                .Where(m=>!m.BestCommentId.HasValue)
                .OrderByDescending(m => m.PostedTime)
                .ToList();

            //当前分类解决高分值
            ViewData["NotAnswerHighCost"] = _contentService.GetTopContentQueable(new ContentQuery
            {
                Channel=Channels.Ask
            })
            .Where(m => !m.BestCommentId.HasValue)
                .OrderByDescending(m => m.Cost)
                .ToList();

            var contents = await _contentService.GetContentsAsync(new ContentPageQuery(Request.Query["page"].ToString().To(1))
            {
                CategoryId = categoryId,
                Channel =  Channels.Ask
            });


            return View(contents);
        }
    }
}
